package com.hqq.concurrency.read_write_lock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.Lock;

/**
 * Reader
 * Created by heqianqian on 2017/7/30.
 */
public class Reader extends Thread {

    private static final Logger LOGGER = LoggerFactory.getLogger(Reader.class);

    private String name;

    private Lock readLock;

    public Reader(String name, Lock readLock) {
        this.name = name;
        this.readLock = readLock;
    }

    @Override
    public void run() {
        readLock.lock();
        try {
            read();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            readLock.unlock();
        }
    }

    private void read() throws InterruptedException {
        LOGGER.info("{} read begin", name);
        Thread.sleep(500);
        LOGGER.info("{} read end", name);
    }
}
